<?php

require_once(WSB_XTENSION_PATH . 'simpleshop/models/DbTable/Article/Rowset.php');
require_once(WSB_XTENSION_PATH . 'simpleshop/models/DbTable/Article/Row.php');
require_once(WSB_XTENSION_PATH . 'simpleshop/models/DbTable/Basket/Rowset.php');
require_once(WSB_XTENSION_PATH . 'simpleshop/models/DbTable/Basket/Row.php');

class Simpleshop_Model_DbTable_Basket_Table extends WSB_Db_Table
{

    protected $_name 			= 'xt_simpleshop_basket';
    protected $_primary 		= 'id';
    protected $_rowsetClass 	= 'Simpleshop_Model_DbTable_Basket_Rowset';
    protected $_rowClass 		= 'Simpleshop_Model_DbTable_Basket_Row';
    protected $_dependentTables = array();
    protected $_referenceMap    = array();

    /**
     * Dem Warenkorb einen Artikel hinzufuegen.
     *
     * Es wird vorausgesetzt, dass es sich bei $article um einen gueltigen Artikel handelt.
     * Es muss also ein entsprechender Eintrag in der Tabelle "xt_simpleshop_article"
     * vorhanden sein und dieser Eintrag darf nicht geloescht und nicht versteckt sein.
     *
     * @param Simpleshop_Model_DbTable_Article_Row $article
     * @return void
     */
    public function addArticle(Simpleshop_Model_DbTable_Article_Row $article)
	{
    	$sessionId = Zend_Session::getId();
    	if(!empty($sessionId)) {
			$select = $this->select();
			$select->where('session_id=?', $sessionId);
			$select->where('article_id=?', $article->id);
			$select->where('deleted=?', 0);
			$select->where('ordered=?', 0);
			$row = $this->fetchRow($select);
			if(empty($row)) {
	    		$row = $this->createRow();
	    		$row->session_id = $sessionId;
	    		$row->article_id = $article->id;
	    		$row->quantity = 1;
			} else {
				$row->quantity++;
			}
	    	$row->save();
    	}
    }

    /**
     * Gibt das Basket-Rowset zurueck inklusive zugehoerigem Artikel-Row.
     * (1 zu 1 Beziehung, siehe Simpleshop_Model_DbTable_Basket_Row::getArticle)
     *
     * @return Simpleshop_Model_DbTable_Basket_Rowset
     */
    public function getRowset()
	{
    	$data = array();
    	$sessionId = Zend_Session::getId();
    	if(!empty($sessionId)) {
			$db = $this->getAdapter();

		    $basketCols = $this->buildColsForSelect('b');

		    $articleTable = new Simpleshop_Model_DbTable_Article_Table();
		    $articleCols = $articleTable->buildColsForSelect('a');

			$select = $db->select();
		    $select->from(array('b' => 'xt_simpleshop_basket'), $basketCols);
		    $select->join(array('a' => 'xt_simpleshop_article'), 'b.article_id=a.id', $articleCols);
		    $select->where('a.deleted=0');
		    $select->where('a.hidden=0');
		    $select->where('b.session_id=?', $sessionId);
		    $select->where('b.deleted=0');
		    $select->where('b.ordered=0');

		    $stmt = $db->query($select);
		    $basketRows = array();
			foreach($stmt->fetchAll() AS $set) {
				$article = array();
				$basket = array();
				foreach($set AS $key=>$val) {
					// Artikel
					if(substr($key, 0, 2) == 'a.') {
						$article[substr($key, 2)] = $val;
					// Basket
					} else {
						$basket[substr($key, 2)] = $val;
					}
				}
				$basket['article'] = $article;
				$basketRows[] = $basket;
			}
	        $data  = array(
	            'table'    => $this,
	            'data'     => $basketRows,
	            'readOnly' => false,
	            'rowClass' => $this->_rowClass,
	            'stored'   => true
	        );
    	}
		return new Simpleshop_Model_DbTable_Basket_Rowset($data);
    }

}